package org.jsbd.boss.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


public class RegsUtils {
	private static final char[] charCode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
	private static final char[] imeiCode = "0123456789".toCharArray();
	private static final char[] KEY = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
			108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 };
	public static int Ds = 8;
	private static int[] Dr = { 0, 5, 3, 4, 6, 7, 2, 1 };
	public static final String SDK_VERSION_120 = "20120";

	private static String reversalString(String str) {

        StringBuffer s = new StringBuffer();
        if ("".equals(str)) {
            return null;
        } else {
            StringBuffer sb = new StringBuffer(str);
            sb.reverse();
            for (int i = 0; i < sb.length(); i++) {
                char c = sb.charAt(i);
                if (isCharacters(c)) {
                } else {
                    c = (char) (c ^ 0x8);
                    if (isCharacters(c)) {
                        c = (char) (c ^ 0x8);
                    }
                }
                s.append(c);
            }
        }
        return s.toString();
    }
	
	private static boolean isCharacters(char c) {
        // 48 57 65 90 97 122
        return ((c < '0') || (c > '9')) && ((c < 'A') || (c > 'Z')) && ((c < 'a') || (c > 'z'));
    }
	
	public static String decode(String str) {
        str = reversalString(str);
        int sLen = str.length();
        char char2[] = new char[sLen];
        int m = 0;
        for (int i = 0; i < char2.length; i++) {
            char2[Dr[i % Ds] + m * Ds] = str.charAt(i);
            if ((i != 0) && ((i + 1) % Ds == 0))
                m++;
        }
        return new String(char2);
    }
	
	public static void main(String[] args) {
        String str = "nl   @ xiEqt7zCR28M@298@0EANW@A3@T0SDK D56KT02@26386359@8588058979@0@890757155231326@80540@1298368848834";
        System.out.println(decode(str));
	}
	
	public static String getUserToken(String imsi, String randomInt) {
        // +Signature-OF-Secret+POST+Encrypt-IMSI=460002939711748&Random-Code=2964
        String signerContent = "+Signature-OF-Secret+POST+Encrypt-IMSI=" + imsi + "&Random-Code=" + randomInt;
        return encryptSHA1(signerContent);
	}
	
	public static String encryptSHA1(String str) {
        String str2 = null;
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(new String("Signature-OF-Secret&").getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(secretKeySpec);
            str2 = new String(encode(mac.doFinal(str.getBytes("UTF-8")))).replace("\r\n", "");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str2;
    }
	
	public static String encode(byte[] data) {
        StringBuffer sb = new StringBuffer();
        int i = data.length;
        int j = 0;
        while (true) {
            if (j >= i)
                ;
            int m;
            int n = 0;
            int i1 = 0;
            int k = j + 1;
            m = 0xFF & data[j];
            if (k == i) {
                sb.append(KEY[(m >>> 2)]);
                sb.append(KEY[((m & 0x3) << 4)]);
                sb.append("==");
            } else {
                n = k + 1;
                i1 = 0xFF & data[k];
                if (n == i) {
                    sb.append(KEY[(m >>> 2)]);
                    sb.append(KEY[((m & 0x3) << 4 | (i1 & 0xF0) >>> 4)]);
                    sb.append(KEY[((i1 & 0xF) << 2)]);
                    sb.append("=");
                    break;
                }
            }
            j = n + 1;
            int i2 = 0xFF & data[n];
            sb.append(KEY[(m >>> 2)]);
            sb.append(KEY[((m & 0x3) << 4 | (i1 & 0xF0) >>> 4)]);
            sb.append(KEY[((i1 & 0xF) << 2 | (i2 & 0xC0) >>> 6)]);
            sb.append(KEY[(i2 & 0x3F)]);
        }
        return sb.toString();
    }
	
	public static String getRandomStr(int len) {
        Random localRandom = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            int j = localRandom.nextInt(62);
            sb.append(String.valueOf(charCode[j]));
        }
        return sb.toString();
    }
	
	public static String getRandomImei(int len) {
        Random localRandom = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            int j = localRandom.nextInt(imeiCode.length);
            sb.append(String.valueOf(imeiCode[j]));
        }
        return sb.toString();
    }
}
